import { Input, Component, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import { Type, Message, TextMessage } from '../../entity/Chat';
import { EventService, EventMessage, EventSource} from '../../services/event.service';
import { Booked, BookedResult, BookedResultO } from 'src/app/entity/TT';
import { RequestReplyService } from 'src/app/services/request-reply.service';



@Component({
  selector: 'app-messagebox',
  templateUrl: './messagebox.component.html',
  styleUrls: ['./messagebox.component.scss']
})
export class MessageboxComponent implements OnInit , OnChanges {

  @Input()
  booked: Booked;

  messages: TextMessage[] = [];

  constructor(
    private eventService: EventService,
    private requestService: RequestReplyService) { }

  ngOnInit() {
    console.log('ngOnInit...');

    if (!this.booked) {

      this.eventService.subscribeMessage((msg: TextMessage) => {
        this.messages.push(msg);
      });
    }
  }

  ngOnChanges(changes: SimpleChanges): void {
    const booked  = changes.booked;
    if (booked) {
      console.log('booked changed');
      const b: Booked = booked.currentValue;
      if (b.result.name === BookedResult.completed ) {

        this.requestService.GetBookedMessage(b).subscribe((list: TextMessage[]) => {
          console.log('GetBookedMessage', list);
          this.messages = list;
        });
      }
    }
  }


}
